var ECar=ECar || {};
ECar.countDown=ECar.countDown||{};

ECar.countDown=(function(){
	var interval,
		defaults={
			id:'countDown',				//倒计时显示对象ID
			step:1000,					//倒计时间隔时间
			start:new Date(),			//倒计时开始时间
			dayUnit:'天',				//倒计时显示天数单位
			hourUnit:'小时',			//倒计时显示小时单位
			minuteUnit:'分',			//倒计时显示分钟单位
			secondUnit:'秒'				//倒计时显示秒数单位
		};		
		//stop或duration属性需要从外部传入，全部传入时以stop为准。stop：倒计时结束时间；duration：倒计时持续时间。

	function initilize(options){		//倒计时对象初始化
		//输入参数合法性判断
		isDate(options['start']) || delete options['start'];
		isDate(options['stop']) || delete options['stop'];

		//输入参数与默认值合并
		for(var opt in defaults){
			if(options[opt]===undefined){
				options[opt]=defaults[opt];
			}
		}
		//检查参数并开始倒计时
		if(checkOptions(options)){
			startCountDown(options);
		}
	}
	//判断参数是否是日期
	function isDate(date){
		return Object.prototype.toString.call(date)==='[object Date]';
	}
	//检查输入参数
	function checkOptions(options){
		if(options['stop']===undefined && options['duration']===undefined){
			return false;
		}
		else if(options['stop'] && options['duration']){
			if(options['stop']-options['start']!==options['duration']){
				options['duration']=options['stop']-options['start'];
			}
		}
		else if(options['duration']){
			options['stop']=new Date(options['start'].getTime()+options['duration']);
		}
		else{
			options['duration']=options['stop']-options['start'];
		}
		return true;
	}
	//计算倒计时显示的数字
	function calculate(options){
		var second=Math.floor(options['timeRest']/1000),
			minute=0,
			hour=0,
			day=0;

		if(second>=86400){
			day=Math.floor(second/86400);
			hour=Math.floor((second-day*86400)/3600);
			minute=Math.floor((second-day*86400-hour*3600)/60);
			second=second%60;
			if(options.step>=86400000){					
				setText(options,day,-1,-1,-1);
			}
			else if(options.step>=3600000){					
				setText(options,day,hour,-1,-1);
			}
			else if(options.step>=60000){					
				setText(options,day,hour,minute,-1);
			}
			else{					
				setText(options,day,hour,minute,second);
			}				
		}
		else if(second>=3600){
			hour=Math.floor(second/3600);
			minute=Math.floor((second-hour*3600)/60);
			second=second%60;
			if(options.step>=3600000){					
				setText(options,-1,hour,-1,-1);
			}
			else if(options.step>=60000){					
				setText(options,-1,hour,minute,-1);
			}
			else{
				setText(options,-1,hour,minute,second);
			}
		}
		else if(second>=60){
			minute=Math.floor(second/60);
			second=second%60;
			if(options.step>=60000){					
				setText(options,-1,-1,minute,-1);
			}
			else{					
				setText(options,-1,-1,minute,second);
			}
			
		}
		else if(second>=0){				
			setText(options,-1,-1,-1,second);
		}
		else{
			setText(options,-1,-1,-1,-1);
		}
	}
	//在指定对象中写入文本
	function setText(options,day,hour,minute,second){
		var text='';
		hour=(hour>=0 && hour<=9)?'0'+hour:hour;
		minute=(minute>=0 && minute<=9)?'0'+minute:minute;
		second=(second>=0 && second<=9)?'0'+second:second;
		text+=(day>=0)?(day+' '+options['dayUnit'])+' ':'';
		text+=(hour>=0)?(hour+' '+options['hourUnit'])+' ':'';
		text+=(minute>=0)?(minute+' '+options['minuteUnit'])+' ':'';
		text+=(second>=0)?(second+' '+options['secondUnit'])+' ':'';			
		options.dom.innerHTML=text;
	}
	//开始倒计时
	function startCountDown(options){			
		options['timeRest']=options['stop']-new Date();
		options['dom']=document.getElementById(options['id']);
		calculate(options);
		interval=setInterval(function(){
			options['timeRest']-=options.step;
			calculate(options);
			if(options['timeRest']<0){
				clearInterval(interval);
				calculate(options);
				options['callback'] && options['callback'](options);
			}
		},options.step);
	}
	//停止倒计时
	function stopCountDown(){
		clearInterval(interval);
	}

	return{
		start:function(options){
			initilize(options);
		},
		stop:function(){
			stopCountDown();
		}
	}

})();